var selectedValues = [];
var inputCheckboxElement = [];
$(function() {
    loadActionSelect();
});
function loadActionSelect() {
    inputCheckboxElement['checkAll'] = $("input[type='checkbox']#selectall");
    inputCheckboxElement['checkRaw'] = $("input[type='checkbox'].ckeckRow");
    inputCheckboxElement.checkRaw.change(function() {
        selectRow($(this).attr('id'));
    });
    inputCheckboxElement.checkAll.change(function() {

        if ($(this).is(':checked'))
        {
            $("input.ckeckRow").prop('checked', true);
        }
        else
        {
            $("input.ckeckRow").prop('checked', false);
        }
        $.each(inputCheckboxElement.checkRaw, function() {
            selectRow($(this).attr('id'));
        });
    });
}
function updateCheckedRaw() {
    $.each(selectedValues, function(index, value) {
        var check_row = $('input#ckb_' + value);
        if (check_row)
        {
            check_row.prop('checked', true);
            check_row.parent().parent().addClass('selected');
        }
    });
    if ($("input.ckeckRow").is(':checked'))
    {
        $("input[type='checkbox']#selectall").prop('checked', true);
    }

}
function selectRow(element)
{

    if ($('input#' + element).is(':checked'))
    {
        $('input#' + element).parent().parent().addClass('selected');
        selectedValues.push($('input#' + element).val());
//        console.log(selectedValues);
    }
    else
    {
        $('input#' + element).parent().parent().removeClass('selected');
        deleteElemen(selectedValues, $('input#' + element).val());
//        console.log(selectedValues);
    }
}
function deleteElemen(contenerArray, val) {
    $.each(contenerArray, function(index, value) {
        if (val == value)
        {
            contenerArray.splice(index, 1);
        }
    });
}

/********************************funciones Ajax*******************************/

function ajaxAgregar() {
    if (selectedValues != '')
    {
        console.log(selectedValues);
        $.ajax({
            type: 'POST',
            url: baseUrl + "campanias/campaniaUsuarioCruge/create",
            dataType: 'json',
            data: {
                usuarios: selectedValues,
                campania_id: entidad_id,
                tipoRol: tipoRol}, //tipoRol = OPERADOR,SUPERVISOR
            success: function(data, textStatus, jqXHR) {
                if (data.success)
                {
                    $("#mainModal").modal('hide');
                    if (tipoRol == "OPERADOR")
                    {
                        ActualizarInformacion("#addOperador-grid");
                    }
                    if (tipoRol == "SUPERVISOR")
                    {
                        ActualizarInformacion("#addSupervisor-grid");
                    }

                }
                else {
                    $("#mainModal").modal('hide');
                    bootbox.alert('Error al agregar los usuarios');
                }
            }
        });
    }
    else
    {
        bootbox.alert('selecione por o menos un usuario');
    }
}
